package com.muyi.base.controller;

import com.muyi.base.annotation.IgnoreLogin;
import com.muyi.base.annotation.LoginUser;
import com.muyi.base.dto.UserReq;
import com.muyi.base.model.Business;
import com.muyi.base.model.User;
import com.muyi.base.service.UserService;
import com.muyi.base.config.jwt.JwtService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Administrator
 * @Date: 2018/11/20 20 08
 * @Description:
 */
@RestController
@RequestMapping(value = "user")
@Api("用户类接口")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private JwtService jwtService;

    @PostMapping("save")
    @ApiOperation("保存用户信息")
    @ApiImplicitParam(name="user", value="用户信息实体类", required = true, dataType = "User")
    public void userSave(UserReq user){
        userService.save(user);
    }

    @PostMapping("redisTest")
    @ApiOperation("redis测试")
    public void saveRedis(@ApiParam(value="redis信息实体类",required = true) UserReq user){
        userService.saveRedis(user);
    }

    @PostMapping("query")
    @ApiOperation("查询用户信息")
    public User query(@ApiParam(value="查询用户信息", required = true) UserReq user){
        return userService.findByName(user.getName());
    }

    @PostMapping("login")
    @ApiOperation("用户登录")
    @IgnoreLogin
    public String login(){
        User user = new User();
        user.setName("1111");
        user.setPasswd("2222");
        user.setUserId("1012");
        return jwtService.generateToken(user);
    }


    @PostMapping("/business")
    public User business(Business business, @LoginUser User user) {//在业务逻辑可以使用注解将我们的user注入进来
        logger.info("用户信息参数id:{},姓名:{}", user.getUserId(), user.getName());
        logger.info("我们的业务参数:{},{}", business.getStr(), business.getNum());
        return user;
    }
}
